package com.smartEdu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.controller.app.AppBaseController;
import com.smartEdu.exception.RequestException;
import com.smartEdu.response.Response;
import com.smartEdu.service.SecurityService;

@Controller
public class SecurityController extends AppBaseController{
	public static final String TAG="SecurityController";
	@Autowired
	@Qualifier(value="appresponse")
	public Response respContext;
	
	@Autowired
	@Qualifier(value="securityServiceImpl")
	public SecurityService securityService;
	
	@RequestMapping(value="/synctoken",method=RequestMethod.POST)
	public void SyncToken(HttpServletRequest request,HttpServletResponse response){
		InitSession(request);
		Session sess=null;
		try {
			sess=GetSession();
			sess.put(Tag_Tips.REQUEST, GetParameter(request));//将requset中的REQUEST	字段放入session
			securityService.SyncToken(sess);
		} 
		catch (RequestException e) {
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.ERROR);
			LogCvt.error("token同步请求参数为空");
		}
		catch (Exception e) {
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.ERROR);
			LogCvt.error("系统异常");
		}
		finally {
			if (sess!=null) {
				sess.remove(Tag_Tips.REQUEST);
			SetResponse(response, sess);
				sess.clear();
				sess=null;
			}
		}
	}

	@Override
	public String GetRespContent(Session sess) {
		return respContext.GetRespContent(sess);
	}
	/**初始化Session因为该controller不受拦截器拦截故手动添加
	 * 
	 */
	public void InitSession(HttpServletRequest request){
		Session sess=new Session();
		request.setAttribute("sess", sess);
	}
}
